package com.person.server;

import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;

//GRPC服务端
public class GrpcServer {
    private Server server;

    public static void main(String[] args) throws IOException, InterruptedException {
        GrpcServer server = new GrpcServer();
        server.start();
        server.awaitTermination();
    }

    private void start() throws IOException {
        //启动服务需要增加PersonServiceImplBase,这是使用重写方法后的PersonServiceImpl
        //PersonServiceImpl需要重写person.proto文件里的PersonService所有方法
        this.server = ServerBuilder.forPort(8899).addService(new PersonServiceImpl()).build().start();
        System.out.println("GRPC Java服务端启动");

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            public void run() {
                System.out.println("关闭JVM");
                GrpcServer.this.stop();
            }
        }));

        System.out.println("GRPC Java服务端启动成功");
    }

    private void stop() {
        if (null != this.server) {
            this.server.shutdown();
        }
    }

    private void awaitTermination() throws InterruptedException {
        if (null != this.server) {
            this.server.awaitTermination();
        }
    }




}